/* Sentinel - IRC Statistical and Operator Services
** hidden.c - Functions to manipulate the hidden list
**
** Copyright W. Campbell and others.  See README for more details
** Some code Copyright: Jonathan George, Kai Seidler, ircd-hybrid Team,
**                      IRCnet IRCD developers.
**
** $Id: hidden.c 7 2010-02-01 19:17:42Z dubkat $
*/

#include "stats.h"
#include "struct.h"
#include "proto.h"
#include "match.h"
#include "slog.h"
#include "mem.h"

/* Perm really isn't used right now, it's there for future expansion */
void AddHidden(char *name, int perm)
{
  struct Hidden *h = smalloc(sizeof(struct Hidden));
  dlink_node *dl = dlink_create();

  h->perm = perm;
  strlcpy(h->name, name, MAXHOST);
  dlink_add(h, dl, &hiddenhead);
  memcounts.hidden++;
}

/* Not needed right now, it will be used when SET is added back */
void DelHidden(char *name)
{
  dlink_node *dl;
  struct Hidden *hd;

  DLINK_FOREACH(dl, hiddenhead.head)
  {
    hd = (struct Hidden *) dl->data;
    if (!strcasecmp(name, hd->name))
    {
      if (!hd->perm)
      {
        dlink_delete(dl, &hiddenhead);
        free(hd);
        memcounts.hidden--;
        dlink_free(dl);
        return ;
      }
      else
      {
        /* It's a permanent hidden server (from conf) */
        return ;
      }
    }
  }

  slog(DEFAULT_LOG, L_ERR, "Unable to remove Hidden list record: %s", name);
}

struct Hidden* findhidden(char *name)
{
  dlink_node *dl;
  struct Hidden *hd;

  debug("findhidden(%s)", name);

  DLINK_FOREACH(dl, hiddenhead.head)
  {
    hd = (struct Hidden *) dl->data;
    if (!match(hd->name, name))
      return hd;
  }

  return NULL;
}
